package dian.qing.li.demo.security.utils;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.stream.Collectors;

@Slf4j
@UtilityClass
public class RequestUtils {

    /**
     * 获取客户端IP EC_liqingdian
     */
    public static String getRequestIp(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException var4) {
                    log.error(var4.getMessage(), var4);
                }
                ipAddress = inet.getHostAddress();
            }
        }
        if (ipAddress != null && ipAddress.length() > 15 && ipAddress.indexOf(",") > 0) {
            ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
        }
        return ipAddress;
    }

    /**
     * 获取请求URI路径
     *
     * @Author: EC_liqingdian
     * @Modify:
     * @Date: 2020/5/7
     */
    public static String getRequestURI(HttpServletRequest request) {
        String requestUri = request.getRequestURI().replaceFirst(request.getContextPath(), "");
        String[] split = requestUri.split("/");
        return Lists.newArrayList(split).stream().filter(StringUtils::isNotBlank).collect(Collectors.joining("/", "/", ""));
    }

    /**
     * 获取域名：http://uc.seedland.cc/
     *
     * @Author: EC_liqingdian
     * @Modify:
     * @Date: 2020/6/5
     */
    public static String getDomain(String requestPath) {
        Assert.isNotBlank(requestPath, "[requestPath]不能为空");
        requestPath = Joiner.on("").join(StringUtils.trim(requestPath), "/");
        boolean expression = StringUtils.startsWith(requestPath, "https://") || StringUtils.startsWith(requestPath, "http://");
        Assert.isTrue(expression, "[requestPath]请求地址格式错误");
        String protocol = StringUtils.substringBefore(requestPath, ":");
        String domain = StringUtils.substringBetween(requestPath, "://", "/");
        return Joiner.on("").join(protocol, "://", domain);
    }
}
